<script>
import request from "@/utils/request";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'practice-mode',
  data() {
    return {
      chapters: [],  // 存储章节数据
      courseId: this.$route.params.courseId,  // 获取路由中的课程ID
    };
  },
  methods: {
     ToPracticeChapter(chapterId,chapterTitle){
      /*this.$router.push({
        path: `/test/${this.courseId}/${chapterId}`,
        query: { title: chapterTitle }
      });*/
       let newUrl = this.$router.resolve({
         path: `/test/${this.courseId}/${chapterId}`,
         query: { title: chapterTitle }
       })
       window.open(newUrl.href, "_blank");
    },
    async fetchChapters() {
      try {
        const response = await request.get(`/getChapterList/${this.courseId}`);
         console.log(response)
        if (response.data.status === 401) {
          // 处理401错误，Token 已经被清除，重定向到登录页
          this.$message({
            message: '身份验证已过期 请重新登录',
            center: true,
            type: 'warning',
            duration: '1500',
            offset: '80',
          });
          // 重定向到登录页面
          this.$router.push('/login');
        } else if (response.status === 200) {
          this.chapters = response.data;  // 保存章节数据
          console.log(this.chapters)
        }
      } catch (error) {
        console.error('获取章节列表失败:', error);
      }
    },
  },
  created() {
    console.log(this.courseId)
    this.fetchChapters();  // 组件创建时获取章节列表
  }
}
</script>

<template>
  <div class="box">
    <div class="content">
      <div class="task-list">
        <div class="class-name">
          <h2>课程章节列表</h2>
        </div>
        <div class="work-list">
          <ul>
            <!-- 动态渲染章节列表 -->
            <li v-for="chapter in chapters" :key="chapter.chapter_id" @click="ToPracticeChapter(chapter.chapter_id,chapter.chapter_title)">
              <div class="icon">
                <!-- 动态渲染章节图片，如果有对应字段 -->
<!--                <img :src="chapter.chapter_icon || './default-icon.png'" alt="章节图标">-->
              </div>
              <div class="right-content">
                <!-- 动态渲染章节标题 -->
                <p class="work-title">{{ chapter.chapter_title }}</p>
<!--                 动态渲染章节状态
                <p class="status">{{ chapter.status }}</p>-->
              </div>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.box {
  position: relative;
  padding: 30px;
  background-color: #F2F4F7;
}
.content{
  width: 100%;
  min-width: 804px;
  border-radius: 6px;
  margin: 0;
  background: #f2f4f7;
  position: relative;
  left: 0;
  top: 0;
}
.task-list {
  min-width: 1040px;
  background: #FFFFFF;
  box-shadow: 0 2px 10px 0 rgba(237, 238, 240, 0.50);
  border-radius: 8px;
  position: relative;
  min-height: 818px;
}
.class-name {
  border-bottom: 1px solid #F2F2F2;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  padding: 19px 30px;
}
.class-name h2 {
  height: 22px;
  font-size: 16px;
  font-weight: 500;
  color: #181E33;
  line-height: 22px;
}
.task-list ul li {
  position: relative;
  padding-left: 30px;
  cursor: pointer;
  list-style-type: none;
}
.task-list ul li:hover{
  background-color: #F7FAFC;
}
.task-list ul {
  display: block;
  margin-block-start: 1em;
  margin-block-end: 1em;
  margin-inline-start: 0px;
  margin-inline-end: 0px;

}
.icon {
  display: inline-block;
}
.right-content {
/*  display: flex;
  align-items: center;*/
  margin-left: 62px;
  padding: 14px 20% 14px 0;
  min-height: 40px;
  border-bottom: 1px solid #F2F2F2;
  line-height: 20px;
  font-size: 14px;
  font-weight: 400;
  color: #181E33;
  max-width: 92%;
}
.right-content p {

}
.work-title {

}
.status {
  font-size: 14px;
  font-weight: 400;
  color: #A8A8B3;
  line-height: 17px;
  margin-top: 4px;
}
.deadline {
  position: absolute;
  top: 50%;
  margin-top: -9px;
  right: 30px;
  font-size: 12px;
  color: #4E97FF;
}
.deadline img {
  width: 14px;
  height: 14px;
  float: left;
  position: relative;
  top: 2px;
  margin-right: 6px;
}
</style>
